import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xiaozhoubg/service/service_method.dart';
import '../../config/service_url.dart';
import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';

class BookCatalog extends StatefulWidget {
  Map arguments;
  BookCatalog({Key key, this.arguments}) : super(key: key);

  @override
  _BookCatalogState createState() => _BookCatalogState(arguments);
}

class _BookCatalogState extends State<BookCatalog> {
  Map arguments;
  _BookCatalogState(this.arguments);
  List bookcatalogList;
  int booklength;
  List sectionList;
  Map book;
  String shardDocs = "第01节：学习手册";
  String shardId = "0";
  Future saveDocs(String docs) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    print("本地缓存节名：$docs");
    sharedPreferences.setString("docs", docs);
    this.getDocs();
  }

  Future saveDocsid(String id) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    print("本地缓存节id：$id");

    sharedPreferences.setString("docsid", id);
    this.getDocsId();
  }

  Future getDocs() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      this.shardDocs = sharedPreferences.get("docs");
    });
  }

  Future getDocsId() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      this.shardId = sharedPreferences.get("docsid");
    });
  }

  @override
  void initState() {
    super.initState();
    this.getDocs();
    this.getDocsId();
    getBookCatalog(arguments['id']).then((value) {
      setState(() {
        this.bookcatalogList = value['bookcatalog'];
        this.book = value['book'];
        this.sectionList = value['section'];
        this.booklength = this.bookcatalogList.length;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      context,
      width: 750,
      height: 1334,
    );
    return Scaffold(
      // appbar 返回和收藏按钮
      appBar: AppBar(
        elevation: 0,
        leading: IconButton(
            icon: Icon(
              Icons.keyboard_arrow_left,
              size: 30,
              color: Colors.black,
            ),
            onPressed: () {
              Navigator.of(context).pop();
            }),
        backgroundColor: Color.fromRGBO(242, 242, 242, 1),
        actions: <Widget>[
          FlatButton(
            color: Color.fromRGBO(242, 242, 242, 1),
            onPressed: () {
              print("用户点击了收藏按钮");
            },
            child: Text("收藏"),
          )
        ],
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.fromLTRB(20, 10, 20, 0),
        child: Column(
          children: <Widget>[
            Container(
              width: ScreenUtil().setWidth(690),
              height: ScreenUtil().setHeight(220),
              child: Row(
                children: <Widget>[
                  Container(
                    width: ScreenUtil().setWidth(180),
                    height: ScreenUtil().setHeight(220),
                    child: Image.network(
                      "${book['img']}",
                      // "${book['img']}",
                      fit: BoxFit.cover,
                    ),
                  ),
                  Container(
                    width: ScreenUtil().setWidth(400),
                    padding: EdgeInsets.fromLTRB(10, 5, 0, 0),
                    height: ScreenUtil().setHeight(220),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        SizedBox(
                          height: ScreenUtil().setHeight(10),
                        ),
                        Text(
                          "${book['name']}",
                          style: TextStyle(fontSize: 18),
                        ),
                        // Text("${book['name']}"),
                        SizedBox(height: ScreenUtil().setHeight(20)),
                        Text(
                          // "作者：${book['writer']}",
                          "作者：快乐的小屁孩",
                          style: TextStyle(fontSize: 16),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            //简介
            Container(
              width: ScreenUtil().setWidth(690),
              padding: EdgeInsets.only(top: ScreenUtil().setHeight(20)),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "简介",
                    style: TextStyle(
                      fontSize: 18,
                    ),
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10)),
                  Text(
                    "测试文本啊测试文本啊测试文本啊测试文本啊测试文本啊测试文本啊测试文本啊测试文本啊测试文本啊测试文本啊测试文本啊",
                    style: TextStyle(
                      fontSize: 14,
                      height: ScreenUtil().setHeight(3),
                      color: Color.fromRGBO(128, 128, 128, 1),
                    ),
                  ),
                ],
              ),
            ),
            //目录
            Container(
              padding: EdgeInsets.only(top: ScreenUtil().setHeight(20)),
              width: ScreenUtil().setWidth(690),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "目录",
                    style: TextStyle(
                      fontSize: 18,
                    ),
                  ),
                  SizedBox(height: ScreenUtil().setHeight(10)),
                  Text(
                    "共 $booklength 章",
                    style: TextStyle(
                      fontSize: 14,
                      color: Color.fromRGBO(128, 128, 128, 1),
                    ),
                  ),
                  Container(
                    width: ScreenUtil().setWidth(690),
                    child: SingleChildScrollView(
                      child: ListView.builder(
                        itemCount: this.bookcatalogList.length,
                        itemBuilder: (context, index) {
                          var sections = _getSections(this.sectionList,
                              "${this.bookcatalogList[index]['id']}");
                          return ExpansionTile(
                            title:
                                Text("${this.bookcatalogList[index]['name']}"),
                            children: sections,
                          );
                        },
                        shrinkWrap: true,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: Container(
        width: ScreenUtil().setWidth(750),
        height: ScreenUtil().setHeight(95),
        color: Color.fromRGBO(242, 242, 242, 1),
        child: Stack(
          children: <Widget>[
            Positioned(
                left: ScreenUtil().setWidth(40),
                top: ScreenUtil().setHeight(37),
                child: Text(
                  "$shardDocs",
                  // "第01节：学习手册",
                  style: TextStyle(
                    color: Color.fromRGBO(153, 153, 153, 1),
                  ),
                )),
            //继续阅读按钮
            Positioned(
              right: 0,
              bottom: 0,
              child: Container(
                width: ScreenUtil().setWidth(190),
                height: ScreenUtil().setHeight(96),
                child: RaisedButton(
                  color: Color.fromRGBO(0, 204, 255, 1),
                  onPressed: () {
                    print("ss");
                    Navigator.pushNamed(context, '/bookdocs',
                        arguments: {"id": "$shardId"});
                    print("$shardId");
                  },
                  child: Text(
                    "继续阅读",
                    style: TextStyle(color: Color.fromRGBO(0, 110, 138, 1)),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _getSections(list, chaperid) {
    List<Widget> widgets = [];
    if (list != null && (list as List).length > 0) {
      (list as List).forEach((f) {
        if ("${f['chapterId']['id']}" == chaperid) {
          widgets.add(
            InkWell(
              child: ListTile(
                title: Text("${f['name']}"),
              ),
              onTap: () {
                this.saveDocs("${f['name']}");
                this.saveDocsid("${f['id']}");
                Navigator.pushNamed(context, '/bookdocs', arguments: {
                  "id": "${f['id']}",
                  "bookCatalogList":this.bookcatalogList,
                  "sectionList":this.sectionList,
                });
                print("${f['id']}");
              },
            ),
          );
        }
      });
    }
    return widgets;
  }
}
